import {Action, Actor} from 'iflux2';
import {SearchType} from '../constant/action-type';
import {fromJS} from 'immutable';

const defaultParam = {pageNum: 1, pageSize: 12, totalCount: 0};

export default class SearchActor extends Actor {
    defaultState() {
        return {
            search: {
                pagination: defaultParam,
                searchValue: ''
            }
        }
    }


    @Action(SearchType.SET_SEARCH)
    setSearchValue(state, value) {
        return state.setIn(['search', 'searchValue'], value);
    }


    @Action(SearchType.CHANGE_SEARCH_PAGE_PARAM)
    changePagination(state, params) {
        return state.updateIn(['search', 'pagination'], (pagination)=>pagination.merge(params));
    }


    @Action(SearchType.RESET_SEARCH_PAGINATION_PARAM)
    resetPagination(state) {
        return state.setIn(['search', 'pagination'], fromJS(defaultParam));
    }


    @Action(SearchType.RESET_SEARCH)
    resetSearch(state) {
        return state.update('search', ()=>{
            return fromJS({
                searchValue: '',
                pagination: defaultParam
            })
        })
    }
}